package org.ethan.practice.netease;

import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.*;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import androidx.annotation.Nullable;
import org.ethan.practice.R;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName: ParticleView
 * @Desc:
 * @Author: ethan
 * @CreateDate: 2019/10/12 15:16
 */
public class ParticleView extends View {

    private Paint mPaint;
    private Bitmap mBitmap;
    private List<Ball> mBalls = new ArrayList<>();
    /**
     * 粒子的直径
     */
    private float diameter = 2f;
    private ValueAnimator mAnimator;

    public ParticleView(Context context) {
        this(context, null);
    }

    public ParticleView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ParticleView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {

        mPaint = new Paint();
        mBitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.dengziqi);

        for (int i = 0; i < mBitmap.getWidth(); i++) {
            for (int j = 0; j < mBitmap.getHeight(); j++) {
                Ball ball = new Ball();
                ball.color = mBitmap.getPixel(i, j);
                ball.centerX = i * diameter + diameter / 2f;
                ball.centerY = j * diameter + diameter / 2f;
                ball.radius = diameter / 2f;

               /* ball.ax = 0f;
                ball.ay = 0.98f;

                // [-20,20)
                ball.vx = (float)(Math.pow(-1,Math.ceil(1000)) * 20 * Math.random());
                ball.vy = rangInt(-15,15);*/

                //速度(-20,20)
                ball.vx = (float) (Math.pow(-1, Math.ceil(Math.random() * 1000)) * 20 * Math.random());
                ball.vy = rangInt(-15, 35);
                //加速度
                ball.ax = 0;
                ball.ay = 0.98f;

                mBalls.add(ball);
            }
        }

        mAnimator = ValueAnimator.ofFloat(0, 1f);
        mAnimator.setDuration(2000);
        mAnimator.setRepeatCount(-1);
        mAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                updateBalls();
                invalidate();
            }
        });

    }

    private float rangInt(int a, int b) {
        int max = Math.max(a,b);
        int min = Math.max(a,b);
        return (float) (min + Math.random() * ( max - min));
    }

    private void updateBalls() {
        for (int index = 0; index < mBalls.size(); index++) {
            if(mBalls.get(index) == null) {
                continue;
            }
            Ball ball = mBalls.get(index);
            ball.centerX += ball.vx;
            ball.centerY += ball.vy;

            ball.vx += ball.ax;
            ball.vy += ball.ay;
        }
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        for (int index = 0; index < mBalls.size(); index++) {
            if (mBalls.get(index) == null) {
                continue;
            }
            Ball ball = mBalls.get(index);
            mPaint.setColor(ball.color);
            canvas.drawCircle(ball.centerX, ball.centerY, ball.radius, mPaint);
        }
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (event.getActionMasked() == MotionEvent.ACTION_DOWN) {
            mAnimator.start();
        }
        return super.onTouchEvent(event);
    }
}
